<?php defined("MAIN_DIR") or die("No direct access");

// create a select list from $array with value = $key and text = $value and select $compare
function selectCreate($array,$key,$value,$compare=""){
	$select = "";

	foreach ($array as $k=>$val) {
		if ($compare != "" && $compare == $val[$key]){
			$select .= "<option selected value='".$val[$key]."'>".$val[$value]."</option>";
		} else {
			$select .= "<option  value='".$val[$key]."'>".$val[$value]."</option>";
		}
	}

	return $select;
}

function word_ending($count,$ending1,$ending2,$ending3,$with=true,$one=true)
{
	if ($count == 1)
		return $one ? $ending1 : "1 $ending1";
	$count = (string)$count;
	$char = (int)$count[strlen($count)-1];
	if ($char == 1)
		$word = $ending1;
	elseif ($char > 1 && $char < 5)
		$word = $ending2;
	elseif ($char >= 5 || $char == 0)
		$word = $ending3;
	
	// если число больше 10
	if (((int)$count)/10 > 1)
	{
		// работаем со строкой, т.к. $count может быть, например 112
		$char = (int)($count[strlen($count)-2].$count[strlen($count)-1]);
		if ($char > 10 && $char < 15)
			$word = $ending3;
	}
	
	return $with ? $count." ".$word : $word;	
}

// send header
function to($url='/',$header=null){
	if ($header){
		header("HTTP/1.1 $header");
	}
	header("Location: $url");
	exit;
}

// function showPages - return links with page numbers depending on $count and $limit
function showPages($count, $limit)
{
	global $route;
	if ($count > $limit) {

		$max_page = ceil($count/$limit); // всего страниц может быть
		$page = $route->GET('page') ? $route->GET('page') : 1;
		if ($page > $max_page)
			$page = $max_page;
		elseif ($page < 0)
			$page = 1;

		$url_query = array();
		$url_arr = parse_url($_SERVER['REQUEST_URI']);
		if (isset($url_arr['query']))
			$url_query = explode("&",$url_arr['query']);

		$new_url = $prev_url = $next_url = "?";
		$tmp_arr = array();

		// находим page=x или пустые строки и стираем
		for ($z=0; $z<count($url_query); $z++)
			if ((strpos($url_query[$z],"page=") === false) && ($url_query[$z] != ""))
				$tmp_arr[] = $url_query[$z];
		$url_query = $tmp_arr;

		// записываем строку обратно
		for ($q = 0; $q < count($url_query); $q++)
		{
			$new_url .= $url_query[$q];
			if ($q < count($url_query)-1)
				$new_url .= "&";
		}
		$next_url = $prev_url = $new_url;

		if (count($url_query) > 0)
			$next_url = $prev_url .= "&";
		// дописываем страницы
		if ($route->GET('page')) {
			$prev_url .= "page=".($page-1);
			$next_url .= "page=".($page+1);
		} else {
			$next_url .= "page=2";
		}

		$next = "";
		$prev = "";

		if ($page < $max_page){
			$next = "<a class='dir p_active' href='".$next_url."'>Следующая &rarr;</a>";
		} else {
			$next = "<span class='dir p_inactive'>Следующая &rarr;</span>";
		}

		if ($page > 1){
			$prev = "<a class='dir p_active' href='".$prev_url."'>&larr; Предыдущая</a>";
		} else {
			$prev = "<span class='dir p_inactive'>&larr; Предыдущая</span>";
		}

		$str = "$prev $next";
		$str .= "<div class='pages'>";
		for ($x = 1; $x <= $max_page; $x++) {
			if ($x == $page){
				$str .= " <span class='page p_current'>".$x."</span> ";
			} else {
				$str .= " <a class='page p_active' href='".$new_url;
				if (count($url_query) > 0)
					$str .= "&";
				$str .= "page=".$x."'>".$x."</a> ";
			}
		}
		$str .= "</div>";

		return "<div id='pages'>$str</div>";
	}
}

function translitIt($str) {

	$tr = array(
		"А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
		"Д"=>"D","Е"=>"E","Ж"=>"J","З"=>"Z","И"=>"I",
		"Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
		"О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
		"У"=>"U","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch",
		"Ш"=>"Sh","Щ"=>"Sch","Ъ"=>"","Ы"=>"Yi","Ь"=>"",
		"Э"=>"E","Ю"=>"Yu","Я"=>"Ya","а"=>"a","б"=>"b",
		"в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
		"з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
		"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
		"с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
		"ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
		"ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
		" "=> "-", "."=> "", ","=> "", "/"=> "_", "\""=> "", "'"=> "", ":"=> "", ";"=> "",
		"["=> "", "]"=> "", "("=> "", ")"=> "", "{"=> "", "}"=> "", "«"=> "", "»"=> "",
		"`"=> "", "~"=> "", "!"=> "", "@"=> "", "#"=> "", "$"=> "", "%"=> "", "^"=> "", "&"=> "", "*"=> "", "="=> "", "["=> "", "\\"=> "", "|"=> "", "/"=> "", "<"=> "", ">"=> "", "?"=> ""
	);
	return strtr($str,$tr);
}

function cropText($text,$limit=500) {

	if (mb_strlen($text,"UTF-8") > $limit) {

		$text = mb_substr($text,0,$limit+1,"UTF-8");
		for ($i = mb_strlen($text,"UTF-8")-1; $i > 0; $i--) {
			if (preg_match("/\s/",mb_substr($text,$i,1,"UTF-8"))){
				if (preg_match("/[a-zA-Zа-яА-Я«»\.,]/",mb_substr($text,$i-1,1,"UTF-8"))){
					break;
				}
			}
		}
		$text = mb_substr($text,0,$i,"UTF-8")." ...";
	}

	return $text;
}

?>